﻿using System;
using System.Data;
using System.IO;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
using System.Collections.Specialized;
using System.Text;
using System.Collections.Generic;
using System.Net;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using System.Xml;
using System.Data.Common;
using System.Collections;
using ASPDotNetSamples;


public partial class PdfPrint : System.Web.UI.Page {

    protected void Page_Load(object sender, EventArgs e) {

        if(true){
       // if (Request.QueryString["OrderID"] != null) {

            imgLogo.Src = Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.LastIndexOf('/')) + "/Images/logo.png";

            imgLine1.Src = imgLine1.Src.Replace("~", Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.LastIndexOf('/')));
            imgLine2.Src = imgLine1.Src.Replace("~", Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.LastIndexOf('/')));

            string CouponCode = Convert.ToString(Request.QueryString["CouponCode"]);
            int OrderID = 196;
                //Convert.ToInt32(Request.QueryString["OrderID"]);
            var dataContext = new DealesDataContext();
            var query = dataContext.PDF_OrderDetails(OrderID);

            foreach (var item in query) {
                lblVoucherId.Text = CouponCode;
                imgDeal.Src = Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.LastIndexOf('/')) + "/Admin/ImageDisplay.aspx?DealID=" + item.DealID;

                lblDealTitle.Text = "Redeem for " + item.Name.ToString();
                lblFootprint.Text = item.FootPrint.ToString();

                var orders = dataContext.Select_OrderByDealIDForReport(item.DealID);
                foreach (var order in orders) {
                    var users = dataContext.Select_UserName(order.userID.ToString());
                    foreach (var user in users) {
                        lblPurchaser.Text = Util.ToTitleCase(user.FirstName + " " + user.LastName);
                    }

                    lblPurchaseTime.Text = "Purchased on " + order.StartDate.ToString("MMM dd, yyyy @ hh:mm tt");
                }

                var vendors = dataContext.Select_VendorByID(Convert.ToInt32(item.VendorID.ToString()));
                foreach (var vendor in vendors) {
                    lblVendorName.Text = vendor.VendorName;

                    lblVendor.Text = vendor.VendorName;
                    if (vendor.Address1 != "")
                        lblVendor.Text += "<br/>" + vendor.Address1 + "<br/>";
                    if (vendor.City != "")
                        lblVendor.Text += vendor.City + ", ";
                    if (vendor.State != "")
                        lblVendor.Text += vendor.State + " ";
                    if (vendor.ZipCode != "")
                        lblVendor.Text += vendor.ZipCode;
                    if (vendor.Phone_Off_ != "")
                        lblVendor.Text += "<br/>" + vendor.Phone_Off_;

                    lblWebSite.Text = "Website: " + vendor.Website;

                    //ucGoogleMap._Address1 = vendor.Address1;
                    //ucGoogleMap._City = vendor.City;
                    //ucGoogleMap._State = vendor.State;
                    //ucGoogleMap._GID = vendor.VendorID.ToString();
                }
            }
        }

        try {

            HtmlForm form = new HtmlForm();
            form.Controls.Add(printcontent);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hTextWriter = new HtmlTextWriter(sw);
            //form.Page = this;
            //form.RenderControl(hTextWriter);
            form.Controls[0].RenderControl(hTextWriter);
            string html = sw.ToString();

            HtmlToPdfBuilder builder = new HtmlToPdfBuilder(PageSize.LETTER.Rotate());
            //builder.BeforeRender += new RenderEvent(HandleBeforeRender);
            builder.PageSize1 = PageSize.LETTER.Rotate();

            HtmlPdfPage first = builder.AddPage();
            first.AppendHtml(html);

            byte[] file = builder.RenderPdf();
            string path = string.Empty;
            string filename = string.Empty;
            path = Server.MapPath("~/PDF/");
            filename = DateTime.Now.Ticks.ToString() + ".pdf";
            File.WriteAllBytes(path + filename, file);
            ShowPdf(path + filename, filename);
            File.Delete(path + filename);


        } catch (Exception ex) {
            string a = ex.Message.ToString();
        }
    }

    void HandleBeforeRender(iTextSharp.text.pdf.PdfWriter writer, Document document) {
        string FooterText = string.Empty;
        FooterText = this.Page.Title;
        FooterText = FooterText + "                             ";
        HeaderFooter footer = new HeaderFooter((new Phrase(FooterText)), true);
        Phrase pp = new Phrase();
        pp.Font.Size = 6;
        document.Footer = footer;
    }

    private void ShowPdf(string path, string filename) {
        Response.ClearContent();
        Response.ClearHeaders();
        Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Convert.ToInt32(Request.QueryString["OrderID"]) + ".pdf");
        Response.TransmitFile(path);
        Response.End();
        Response.Flush();
        Response.Clear();
    }
}
